package org.sws.types.model;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.sws.types.enums.ResponseCode;
import org.sws.types.exception.AppException;

import java.io.Serializable;

/**
 * @author sws
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Response<T> implements Serializable {

    private String code;
    private String info;
    private T data;

    public static <T> Response<T> success(T data) {
        return new Response<>(ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.getInfo(), data);
    }
    public static <T> Response<T> success() {
        return success(null);
    }
    public static <T> Response<T> fail(AppException exception) {
        return new Response<>(exception.getCode(), exception.getInfo(), null);
    }
    public static <T> Response<T> fail(Exception exception) {
        return new Response<>(ResponseCode.UN_ERROR.getCode(),exception.getMessage(), null);
    }
    public static <T> Response<T> fail(T data) {
        return new Response<>(ResponseCode.UN_ERROR.getCode(),ResponseCode.UN_ERROR.getInfo(),data);
    }
    public static <T> Response<T> fail() {
        return new Response<>(ResponseCode.UN_ERROR.getCode(),ResponseCode.UN_ERROR.getInfo(),null);
    }

}
